package com.xuecheng.manage_course.controller;

import com.xuecheng.api.course.CourseControllerApi;
import com.xuecheng.framework.domain.cms.response.QueryResponseResult;
import com.xuecheng.framework.domain.course.*;
import com.xuecheng.framework.domain.course.ext.CourseView;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.domain.course.request.CourseListRequest;
import com.xuecheng.framework.domain.course.response.AddCourseResult;
import com.xuecheng.framework.domain.course.response.CoursePreviewResult;
import com.xuecheng.framework.domain.course.response.CoursePublishResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.utils.XcOauth2Util;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_course.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ShengJinFeng
 * @version V1.0
 * @ClassName TeacherplanController
 * @Description (这里用一句话描述这个类的作用)
 * @date 2020/2/1 13:24
 * @since V1.0
 */
@RestController
@Api(value="cms配置管理接口",description = "cms配置管理接口，提供数据模型的管理、查询接口")
@RequestMapping("/course")
public class CourseController extends BaseController implements CourseControllerApi {

    @Autowired
    private CourseService courseService;

    @Override
    @ApiOperation("查询我的课程列表")
    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult findCourseList(@PathVariable("page") int page,@PathVariable("size") int size, CourseListRequest courseListRequest) {
        //获取当前登录用户得id
        XcOauth2Util xcOauth2Util = new XcOauth2Util();
        XcOauth2Util.UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
        if (userJwt == null){
            ExceptionCast.cast(CommonCode.UNAUTHENTICATED);
        }
        String companyId = userJwt.getCompanyId();
        courseListRequest.setCompanyId(companyId);
        return courseService.findCourseList(page,size,courseListRequest);
    }

    @Override
    @ApiOperation("根据课程id查询教学计划列表")
    @PreAuthorize("hasAuthority('coures_teachplan_list')")
    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachplanList(@PathVariable("courseId") String courseId) {
        return courseService.findTeachplanList(courseId);
    }

    @Override
    @ApiOperation("添加课程计划")
    @PreAuthorize("hasAuthority('coures_teachplan_add')")
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        courseService.addTeachplan(teachplan);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    @Override
    @ApiOperation("添加课程基本信息")
    @PostMapping("/coursebase/add")
    public AddCourseResult addCourseBase(@RequestBody CourseBase courseBase) {
        return courseService.addCourse(courseBase);
    }

    @Override
    @ApiOperation("根据课程id获取课程基础信息")
    @PreAuthorize("hasAuthority('course_get_baseinfo')")
    @GetMapping("coursebase/get/{courseId}")
    public CourseBase getCourseBaseById(@PathVariable("courseId") String courseId) {
        return courseService.getCourseBaseById(courseId);
    }

    @Override
    @ApiOperation("更新课程基础信息")
    @PutMapping("coursebase/update")
    public ResponseResult updateCourseBase(@RequestBody CourseBase courseBase) {
        return courseService.updateCourseBase(courseBase);
    }

    @Override
    @ApiOperation("获取课程营销信息")
    @GetMapping("coursemarket/get/{courseId}")
    public CourseMarket getCourseMarketById(@PathVariable("courseId") String courseId) {
        return courseService.getCourseMarketById(courseId);
    }

    @Override
    @ApiOperation("更新课程营销信息")
    @PutMapping("coursemarket/update")
    public ResponseResult updateCourseMarket(@RequestBody CourseMarket courseMarket) {
        return courseService.updateCourseMarket(courseMarket);
    }

    @Override
    @ApiOperation("添加课程图片")
    @PostMapping("/coursepic/add")
    public ResponseResult addCoursePic(@RequestParam("courseId") String courseId,@RequestParam("pic") String pic) {
        return courseService.addCoursePic(courseId,pic);
    }

    @Override
    @ApiOperation("获取课程图片")
    @PreAuthorize("hasAuthority('course_find_pic')")
    @GetMapping("/coursepic/list/{courseId}")
    public CoursePic getCoursePic(@PathVariable("courseId") String courseId) {
        return courseService.findCoursePicById(courseId);
    }

    @Override
    @ApiOperation("删除课程图片")
    @DeleteMapping("/coursepic/delete")
    public ResponseResult deleteCoursePic(@RequestParam("courseId") String courseId) {
        return courseService.deleteCoursePic(courseId);
    }

    @Override
    @ApiOperation("课程视图查询")
    @GetMapping("/courseview/{id}")
    public CourseView findCourseView(@PathVariable("id") String id) {
        return courseService.getCourseViewById(id);
    }

    @Override
    @ApiOperation("预览课程")
    @PostMapping("/preview/{id}")
    public CoursePreviewResult preview(@PathVariable("id") String id) {
        return courseService.preview(id);
    }

    @Override
    @ApiOperation("发布课程")
    @PostMapping("/publish/{id}")
    public CoursePublishResult publish(@PathVariable("id") String id) {
        return courseService.publish(id);
    }

    @Override
    @ApiOperation("查询课程信息列表")
    @GetMapping("/coursepub/list")
    public List<CoursePub> findCoursePubList() {
        return courseService.findCoursePubList();
    }

    @Override
    @ApiOperation("保存课程计划的媒资信息")
    @PostMapping("/savemedia")
    public ResponseResult saveTeachplanMedia(@RequestBody TeachplanMedia teachplanMedia) {
        return courseService.saveTeachplanMedia(teachplanMedia);
    }

    @Override
    @ApiOperation("获取所有课程计划的媒资信息")
    @GetMapping("/teachplanMedia/list")
    public List<TeachplanMedia> findTeachplanMedias() {
        return courseService.findTeachplanMedias();
    }

}
